package ide.ccf.ccproject.wizards;


import ide.ccf.ccproject.CCFProject;
import ide.ccf.ccproject.ProjectCreator;
import ide.ccf.ccproject.ProjectPlugin;
import ide.ccf.ccproject.preferences.CCFPreferencePage;
import ide.ccf.ccproject.preferences.PreferenceConstants;
import ide.ccf.ccproject.translate.Message;
import ide.ccf.ccproject.wizards.pages.ImportQtWizardPage;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescriptionManager;
import org.eclipse.cdt.core.settings.model.ICResourceDescription;
import org.eclipse.cdt.internal.core.envvar.EnvironmentVariableManager;
import org.eclipse.cdt.internal.core.envvar.UserDefinedEnvironmentSupplier;
import org.eclipse.cdt.ui.newui.MultiCfgContributedEnvironment;
import org.eclipse.cdt.utils.envvar.StorableEnvironment;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;

/**
 * 导入Qt项目
 *
 */
public class ImportQtWizard extends Wizard implements IImportWizard, IExecutableExtension
{
	ImportQtWizardPage mainPage;
	IWorkbench workbench;
	IConfigurationElement configuration;
	private static final String SEPARATOR = System.getProperty("path.separator", ";"); //$NON-NLS-1$ //$NON-NLS-2$
	ICConfigurationDescription cfg = null;
	private StorableEnvironment vars = null;
	private static final UserDefinedEnvironmentSupplier fUserSupplier = EnvironmentVariableManager.fUserSupplier;

	/**
	 * 构造器
	 */
	public ImportQtWizard() 
	{
		super();
	}

	/**
	 * 完成向导
	 */
	public boolean performFinish() 
	{
		IProject hProject = ResourcesPlugin.getWorkspace().getRoot().getProject(mainPage.getProjectName());
		// $ADD-CCF-MENCIUS-1$
		String defaultVersion = ProjectPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.CCFVERSION_DEFAULT);
		String binPath = CCFPreferencePage.getCCFVersionBinPath(defaultVersion);
		
		if (binPath == null) 
		{
			PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() 
			{
				public void run() 
				{
					Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
					MessageDialog dialog = new MessageDialog(shell, "构建错误", null, "未设置默认CCF版本。打开首选项对话框中的CCF页面并添加CCF版本。", MessageDialog.ERROR, new String[] { "打开首选项", IDialogConstants.CANCEL_LABEL }, 0);
			        
					if (dialog.open() == 1)
					{
						System.out.println("cancle");
						return;
					}
					PreferencesUtil.createPreferenceDialogOn(shell, "ide.ccf.ccproject.preferences.CCFPreferencePage", null, null).open();
				}
			});

			if (hProject != null) 
			{
				CCFProject cCFProject = new CCFProject(hProject);
				binPath = cCFProject.getBinPath();
			} 
			else 
			{
				defaultVersion = ProjectPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.CCFVERSION_DEFAULT);
				binPath = CCFPreferencePage.getCCFVersionBinPath(defaultVersion);
			}
		}
				
		if(binPath == null)
			return false;
				
		ICResourceDescription cfgd = null;
		ICConfigurationDescription cfgDescription = null;
	
		ICProjectDescription des = CoreModel.getDefault().getProjectDescription(hProject, false);
		if(des != null)
		{
			cfgDescription = des.getDefaultSettingConfiguration();
			IPath projectPath = Platform.getLocation();
			cfgd = cfgDescription.getResourceDescription(projectPath, false);
		}
		//针对CCF增加相应路径到PATH环境变量
		ICConfigurationDescription[] cfgs = null;
		if(cfgDescription != null)
			cfgs = new ICConfigurationDescription[] {cfgDescription};
		if (cfgDescription == null && vars == null)
			vars = fUserSupplier.getWorkspaceEnvironmentCopy();
		if (cfgd == null)
			vars.createVariable("PATH", binPath,
					IEnvironmentVariable.ENVVAR_PREPEND, SEPARATOR);
		else
			for (ICConfigurationDescription cfg : cfgs) {
				MultiCfgContributedEnvironment ce = new MultiCfgContributedEnvironment();
				ce.addVariable("PATH", binPath,
						IEnvironmentVariable.ENVVAR_PREPEND,
						SEPARATOR, cfg);
			}
	
		ProjectCreator procreator = new ProjectCreator(hProject, mainPage.getProFileLocation());
		
		procreator.create(getContainer());
		
		BasicNewProjectResourceWizard.updatePerspective(configuration);	// 切换透视图
		BasicNewResourceWizard.selectAndReveal(hProject, workbench.getActiveWorkbenchWindow());	// 选中新导入项目
		
		IPreferenceStore store = ProjectPlugin.getDefault().getPreferenceStore();
		store.setValue(PreferenceConstants.CCFRUNTIME_VERSION, mainPage.c_runtime.getText().trim() == null ? "" : mainPage.c_runtime.getText().trim());
		store.setValue(PreferenceConstants.CCFSDK_VERSION, mainPage.c_sdk.getText().trim() == null ? "" : mainPage.c_sdk.getText().trim());
        return true;
	}

	/**
	 * 初始化
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) 
	{
		this.workbench = workbench;
		setWindowTitle(Message.ImportQtWizard_0);
		setNeedsProgressMonitor(true);
		mainPage = new ImportQtWizardPage(Message.ImportQtWizard_1);
	}
	
	/**
	 * 添加页面到向导
	 */
    public void addPages() 
    {
        super.addPages(); 
        addPage(mainPage);        
    }

    /**
     * 设置初始配置数据
     */
	public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException 
	{
		configuration = config;
	}
	
	public  void addCCFNature()
	{
		
	}

}
